package cn.quevo.core.file;

import cn.quevo.core.lang.DateUtils;
import cn.quevo.core.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 文件工具
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018-03-09 15:05:46
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 * @return
 */
public class FileUtils {
    private final static Logger log = LoggerFactory.getLogger(FileUtils.class);

    /**
     * 如果目录不存在，则创建
     *
     * @param disk
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:08:20
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static Path createAndNotExists(String disk) throws IOException {
        return createAndNotExists(Paths.get(disk));
    }

    /**
     * 如果目录不存在，则创建
     *
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:08:20
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static Path createAndNotExists(Path path) throws IOException {
        if (Files.notExists(path)) {
            Files.createDirectories(path);
        }
        return path;
    }

    /**
     * 获取文件名后缀
     *
     * @param fileName
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:09:50
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static String getFileSuffix(String fileName) {
        if (StringUtils.isBlank(fileName) || fileName.lastIndexOf(".") == -1) {
            return "";
        }
        if (fileName.startsWith(".")) {
            return fileName;
        }
        return fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
    }

    /**
     * 根据原始名称获取以时间为单位的新名称
     *
     * @param orgFileName
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:10:07
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static String getFileNameCurTime(String orgFileName) {
        return DateUtils.getCurrentTime("yyyyMMddHHmmssSSS").concat(getFileSuffix(orgFileName));
    }

    /**
     * 文件本地复制/保存
     *
     * @param input
     * @param fileName
     * @param savePath
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:10:17
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static int copyLocation(InputStream input, String fileName, String savePath) throws IOException {
        if (StringUtils.isBlank(fileName) || StringUtils.isBlank(savePath)) {
            return 0;
        }
        Files.copy(input, createAndNotExists(savePath).resolve(fileName).normalize());
        return 1;
    }

    /**
     * 文件本地复制/保存
     *
     * @param input
     * @param fileName
     * @param copys
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:10:30
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static int copyLocation(InputStream input, String fileName, String... copys) throws IOException {
        int count = 0;
        if (copys != null && copys.length > 0) {
            for (String savePath : copys) {
                count += copyLocation(input, fileName, savePath);
            }
        }
        return count;
    }

    /**
     * 文件本地复制/保存
     *
     * @param file
     * @param fileName
     * @param savePath
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:10:55
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static int copyLocation(Path file, String fileName, String savePath) throws IOException {
        if (StringUtils.isBlank(fileName) || StringUtils.isBlank(savePath)) {
            return 0;
        }
        Path path = Paths.get(savePath);
        if (Files.notExists(path)) {
            Files.createDirectories(path);
        }
        path = path.resolve(fileName).normalize();
        Files.copy(file, path);
        return 1;
    }

    /**
     * 文件本地复制/保存
     *
     * @param file
     * @param fileName
     * @param copys
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:11:16
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static int copyLocation(Path file, String fileName, String... copys) throws IOException {
        int count = 0;
        if (copys != null && copys.length > 0) {
            for (String savePath : copys) {
                count += copyLocation(file, fileName, savePath);
            }
        }
        return count;
    }

    /**
     * 删除文件
     *
     * @param root     原始文件路径
     * @param trash    回收路径
     * @param fileName 原始文件名称
     * @param thums    备份文件
     * @author July july_sky@foxmail.com
     * @date 2017年8月30日 下午6:16:20
     */
    public static void deleteFile(String root, String trash, String fileName, String... thums) {
        try {
            createAndNotExists(trash);
            Path _del = Paths.get(root, "/", fileName);
            if (Files.exists(_del)) {
                FileUtils.copyLocation(_del, fileName, trash);
                Files.delete(_del);
            }
            if (thums == null || thums.length <= 0) {
                return;
            }
            String thumName = "";
            for (String thum : thums) {
                if (StringUtils.isBlank(thum)) {
                    continue;
                }
                thumName = formatImg(fileName, thum);
                _del = Paths.get(root, "/", thumName);
                if (Files.exists(_del)) {
                    FileUtils.copyLocation(_del, thumName, trash);
                    Files.delete(_del);
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 根据原始文件名称获取新的文件名
     *
     * @param img
     * @param name
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 15:14:07
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static String formatImg(String img, String name) {
        if (StringUtils.isBlank(img) || StringUtils.isBlank(name)) {
            return img;
        }
        String suffix = FileUtils.getFileSuffix(img);
        return img.split("\\.")[0] + "_" + name + suffix;
    }
}
